<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <h1>购物车的功能：</h1>
        <table>
            <tr>
                <td>
                    <input type="checkbox" v-model="isAllChecked"  >
                </td>
                <td>编号</td>
                <td>名称</td>
                <td>单价</td>
                <td>数量</td>
                <td>小计</td>
                <td>操作</td>
            </tr>
            <tr v-for="(goods,index) in goodslist" :key="goods.id">
                <td>
                    <input type="checkbox" v-model="goods.isChecked" >
                </td>
                <td>{{goods.id}}</td>
                <td>{{goods.name}}</td>
                <td>{{goods.price}}</td>
                <td>
                    <input type="button" value=" － " :disabled="goods.count==1" @click="reduceCount(goods)">
                    {{goods.count}}
                    <input type="button" value=" ＋ " :disabled="goods.count==goods.limit" @click="addCount(goods)">
                </td>
                <td>{{goods.count*goods.price}}</td>
                <td>
                    <input type="button" value="删除" @click="deleteGoods(index)">
                </td>
            </tr>
        </table>
        <p>总金额：¥：{{totalMoney()}}</p>
    </div>
</body>

</html>
<script src="./js/vue2.js"></script>
<script>
    // 1、显示购物车中的数据【ok】
    // 2、加减按钮的功能【ok】
    // 3、全选和反向选择【ok】
    // 4、总金额【ok】
    // 5、删除购物车数据【ok】

    let vm = new Vue({
        el: "#app",
        data: {
            goodslist: [
                {
                    isChecked: false,
                    id: "01001",
                    name: "炒细面",
                    price: 12,
                    count: 1,
                    limit: 10
                },
                {
                    isChecked: false,
                    id: "01002",
                    name: "麻辣鸡",
                    price: 45,
                    count: 2
                },
                {
                    isChecked: false,
                    id: "01003",
                    name: "钵钵鸡",
                    price: 1,
                    count: 10
                },
                {
                    isChecked: false,
                    id: "01004",
                    name: "油泼面",
                    price: 11,
                    count: 2
                }
            ]
        },
        computed:{
            isAllChecked:{
                get:function(){
                    return this.goodslist.every(item=>item.isChecked);
                },
                set:function(newVal){
                    this.goodslist.forEach(item=>item.isChecked = newVal);
                }
            }
        },
        methods: {
            addCount(goods) {
                if (goods.limit) {
                    if (goods.count < goods.limit) {
                        goods.count++;
                    }
                } else {
                    goods.count++;
                }
            },
            reduceCount(goods) {
                if (goods.count <= 1) {
                    goods.count = 1;
                    return;
                }
                goods.count--;
            },
            deleteGoods(index) {
                if (confirm("亲，您真的要删除吗？")) {
                    this.goodslist.splice(index, 1);
                    this.checkGoods();
                }
            },
            totalMoney() {
                console.log("totalMoney");
                let money = 0;
                this.goodslist.forEach(item => {
                    if (item.isChecked) {
                        money += item.count * item.price;
                    }
                });
                return money;
            }   
        }
    });




</script>